package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityQuoteBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.enterprise.SecerTitleEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter6;
import com.sgb.kjwl.view.ui.fragment.enterprise.ProfessionalQuoteFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.ServiceFragment;
import com.sgb.kjwl.viewmodel.enterprise.ProfessionalQuoteModel;
import com.swgk.core.base.di.AppComponent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


public class ProfessionalQuoteActivity extends AppActivity {
    @Inject
    ProfessionalQuoteModel viewModel;

    private ActivityQuoteBinding binding;

    private ArrayList<Fragment> mFragments;
    List<ParentAreaEntity<List<ParentAreaEntity>>> categoryDatas2;
    List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> cityDatas2;
    private int index = 0;
    private SecerTitleEntity entity;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder().appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ProfessionalQuoteActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context mContext, int position) {
        Intent intent = new Intent();
        intent.setClass(mContext, ProfessionalQuoteActivity.class);
        intent.putExtra("position", position);
        mContext.startActivity(intent);
    }


    public static void start(Context mContext, SecerTitleEntity entity) {
        Intent intent = new Intent();
        intent.setClass(mContext, ProfessionalQuoteActivity.class);
        intent.putExtra("data", (Serializable) entity);
        mContext.startActivity(intent);
    }


    @Override
    protected void init() {

        entity = (SecerTitleEntity) getIntent().getSerializableExtra("data");
        index = getIntent().getIntExtra("position", 0);
        mFragments = new ArrayList<>();
        categoryDatas2 = new ArrayList<>();
        cityDatas2 = new ArrayList<>();
        viewModel.getServiceType();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_quote);

    }

    public void showTypeView(List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> list) {
        ParentAreaEntity data = new ParentAreaEntity();
        data.setName("全部");
        data.setId("");
        list.add(0,data);

        for (int i = 0; i < list.size(); i++) {

            if (entity != null && entity.getCategoryFirstName().equals(list.get(i).getName()) && list.get(i).getId().equals(entity.getCategoryFirstId() + "")) {
                index = i;
                mFragments.add(ProfessionalQuoteFragment.newInstance(list.get(i).getChildren(), list.get(i).getId(), entity));
            } else {
                if(entity!=null){
                    mFragments.add(ProfessionalQuoteFragment.newInstance(list.get(i).getChildren(), list.get(i).getId()));
                }else {
                    mFragments.add(ServiceFragment.newInstance(list.get(i).getChildren(), list.get(i).getId()));
                }
            }
        }

        CommPagerAdapter6 pagerAdapter = new CommPagerAdapter6(getSupportFragmentManager(), mFragments, list);
        binding.viewpager.setOffscreenPageLimit(list.size());

        binding.viewpager.setAdapter(pagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.tabLayout.getTabAt(index).select();

    }
}
